﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Lcsoft.UrlRewriter.Utilities;

namespace Lcsoft.UrlRewriter
{
	/// <summary>
	/// 替换 &lt;asp:form&gt; 以支持重写 postback。
	/// </summary>
	/// <remarks>
	/// <code>
	/// &lt;%@ Page language="c#" Codebehind="MyPage.aspx.cs" AutoEventWireup="false" Inherits="MyPage" %&gt;
	/// &lt;%@ Register TagPrefix="url" Namespace="Lcsoft.UrlRewriter" Assembly="Lcsoft.UrlRewriter" %&gt;
	/// &lt;html&gt;
	/// ...
	/// &lt;body&gt;
	/// &lt;url:form id="MyForm" runat="server"&gt;
	/// ...
	/// &lt;/url:form&gt;
	/// &lt;/body&gt;
	/// &lt;/html&gt;
	/// </code>
	/// </remarks>
	[ComVisible(false)]
	[ToolboxData("<{0}:Form runat=server></{0}:RewrittenForm>")]
	public class Form : HtmlForm
	{
		/// <summary>
		/// 呈现 <see cref="Form"/> 控件的子控件。
		/// </summary>
		/// <param name="writer">接收呈现内容的 <see cref="HtmlTextWriter"/>。</param>
		protected override void RenderChildren(HtmlTextWriter writer)
		{
			writer.RenderBeginTag(HtmlTextWriterTag.Div);

			base.RenderChildren(writer);

			writer.RenderEndTag();
		}

		/// <summary>
		/// 将 <see cref="Form"/> 控件的特性呈现给指定的 <see cref="HtmlTextWriter"/> 对象。
		/// </summary>
		/// <param name="writer">接收呈现内容的 <see cref="HtmlTextWriter"/>。</param>
		protected override void RenderAttributes(HtmlTextWriter writer)
		{
			writer.WriteAttribute(Constants.AttrName, this.Name);

			this.Attributes.Remove(Constants.AttrName);

			writer.WriteAttribute(Constants.AttrMethod, this.Method);

			this.Attributes.Remove(Constants.AttrMethod);

			writer.WriteAttribute(Constants.AttrAction, HttpModule.RawUrl, true);

			this.Attributes.Remove(Constants.AttrAction);
			this.Attributes.Render(writer);

			if (this.ID != null) writer.WriteAttribute(Constants.AttrId, this.ClientID);
		}
	}
}
